const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    post: {
      id: 0,
      linkMan: '',
      linkPhone: '',
      fullAddress: '',
      status: '0'
    },
    pid: 0
  },
  bindinputMobile(event) {
    let address = this.data.post;
    address.linkPhone = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputName(event) {
    let address = this.data.post;
    address.linkMan = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputAddress(event) {
    let address = this.data.post;
    address.fullAddress = event.detail.value;
    this.setData({
      address: address
    });
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options)
    if (options.pid) {
      this.setData({
        pid: options.pid
      });
    }


  },
  saveAddress() {
    console.log(this.data.post)
    let address = this.data.post;

    if (!address.linkMan) {
      util.showErrorToast('请输入收货人姓名');

      return false;
    }

    if (!address.linkPhone) {
      util.showErrorToast('请输入手机号码');
      return false;
    }
    if (!util.checkPhone(address.linkPhone)) {
      util.showErrorToast('请输入正确的手机号码');
      return false;
    }

    if (!address.fullAddress) {
      util.showErrorToast('请输入详细地址');
      return false;
    }


    let pid = this.data.pid;
    util.requestPost(api.SavePostUrl, {
      linkMan: address.linkMan,
      linkPhone: address.linkPhone,
      fullAddress: address.fullAddress
    }).then(function(res) {
      if (res.success) {
        if (pid == 0) {
          wx.redirectTo({
            url: '/pages/vip/address/address'
          })
        } else {
          wx.navigateTo({
            url: '/pages/goods/order/order?postId=' + res.data + '&pid=' + pid
          })
        }

      } else {
        util.showErrorToast(res.data);
      }
    });
  }
})